package com.swe727.spring2011.constraint1.server;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;

import com.swe727.spring2011.constraint1.coordinate.Coordinate;
import com.swe727.spring2011.constraint1.observation.Observation;
import com.swe727.spring2011.constraint1.observation.ObservationDataStore;

/**
 * The remote object instance for retrieving observations.
 * 
 * @author Fernando Boccanera
 * 
 */
public class GetObservationHandler implements GetObservationRemote {

	private ObservationDataStore STORE;

	/**
	 * Create a new handler which will get observations for a particular
	 * coordinate
	 * 
	 * @param dataStore
	 *            from which observations are gotten.
	 */
	public GetObservationHandler(ObservationDataStore dataStore) {
		STORE = dataStore;
	}

	/**
	 * getObservation method
	 */
	public HashSet<Observation> getObservation(Coordinate coordinate) {
		HashSet<Observation> obsAtCoord = new HashSet<Observation>();
		obsAtCoord = STORE.getAll(coordinate);
		Iterator iterator = obsAtCoord.iterator();
		Date timeNow = new Date();
		while (iterator.hasNext()) {
			Observation obs = (Observation) iterator.next();
			if (timeNow.getTime() - obs.getTimestamp().getTime() > 3600000) {
				iterator.remove();
			}
		}
		return obsAtCoord;
	}
}
